package com.ruoyi.web.controller.hh;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.domain.HhBrand;
import com.ruoyi.domain.HhOrderDetail;
import com.ruoyi.domain.HhOrderDetailVO;
import com.ruoyi.domain.HhType;
import com.ruoyi.service.IHhBrandService;
import com.ruoyi.service.IHhOrderDetailService;
import com.ruoyi.service.IHhOrderService;
import com.ruoyi.service.IHhTypeService;
import com.ruoyi.utils.PrintPageUtil;
import com.ruoyi.utils.SelectHelpUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 销售订单明细Controller
 * @author chongjin
 * @date 2020-11-18
 */
@Controller
public class HhOrderDetailController extends BaseController {
    private String prefix = "hh/saleManage/order";

    @Autowired
    private IHhTypeService hhTypeService;
    @Autowired
    private IHhBrandService hhBrandService;
    @Autowired
    private IHhOrderService hhOrderService;
    @Autowired
    private IHhOrderDetailService hhOrderDetailService;

    /**
     * 查询销售订单明细列表
     */
    @PostMapping("/order/detail/list")
    @ResponseBody
    public TableDataInfo list(HhOrderDetailVO hhOrderDetailVO) {
        startPage();
        List<HhOrderDetailVO> list = hhOrderDetailService.selectHhOrderDetailList(hhOrderDetailVO);
        return getDataTable(list);
    }

    /**
     * 获取销售商品页面
     */
    @GetMapping("/order/detail/add/{orderId}")
    public String add(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List<HhType> typeList = hhTypeService.selectHhTypeList(hhType);
        List<HhBrand> brandList = hhBrandService.selectHhBrandList(null);

        HhOrderDetailVO hhOrderDetail = new HhOrderDetailVO();
        hhOrderDetail.setOrderId(orderId);
        List<HhOrderDetailVO> list = hhOrderDetailService.selectHhOrderDetailList(hhOrderDetail);
        int selectLength = list == null ? 0 : list.size();
        mmap.put("brandList", SelectHelpUtil.getNameBrand(brandList));
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        mmap.put("selectLength", PrintPageUtil.pageSize - selectLength);
        mmap.put("orderId", orderId);
        return prefix + "/product_choose";
    }

    /**
     * 获取退货商品页面
     */
    @GetMapping("/order/detail/addReturn/{orderId}")
    public String addReturn(@PathVariable("orderId") String orderId, ModelMap mmap) {
        HhType hhType = new HhType();
        hhType.setParentId(Long.parseLong("0"));
        List<HhType> typeList = hhTypeService.selectHhTypeList(hhType);
        List<HhBrand> brandList = hhBrandService.selectHhBrandList(null);

        HhOrderDetailVO hhOrderDetail = new HhOrderDetailVO();
        hhOrderDetail.setOrderId(orderId);
        List<HhOrderDetailVO> list = hhOrderDetailService.selectHhOrderDetailList(hhOrderDetail);
        int selectLength = list == null ? 0 : list.size();
        mmap.put("brandList", SelectHelpUtil.getNameBrand(brandList));
        mmap.put("parentType", SelectHelpUtil.getNameType(typeList));
        mmap.put("selectLength", PrintPageUtil.pageSize - selectLength);
        mmap.put("hhOrder", hhOrderService.selectHhOrderById(orderId));
        return prefix + "/product_return_choose";
    }

    /**
     * 保存销售订单明细
     */
    @Log(title = "保存关闭", businessType = BusinessType.UPDATE)
    @PostMapping("/order/detail/save")
    @ResponseBody
    public AjaxResult saveOrderDetail(HhOrderDetailVO hhOrderDetailVO) {
        return AjaxResult.success(hhOrderDetailService.saveOrderDetail(hhOrderDetailVO));
    }

    /**
     * 新增保存销售订单明细
     */
    @PostMapping("/order/detail/add")
    @ResponseBody
    public AjaxResult addSave(HhOrderDetailVO hhOrderDetailVO) {
        return toAjax(hhOrderDetailService.insertHhOrderDetail(hhOrderDetailVO));
    }

    /**
     * 修改保存销售订单明细
     */
    @Log(title = "修改销售明细", businessType = BusinessType.UPDATE)
    @PostMapping("/order/detail/edit")
    @ResponseBody
    public AjaxResult editSave(HhOrderDetail hhOrderDetail) {
        return toAjax(hhOrderDetailService.updateHhOrderDetail(hhOrderDetail));
    }

    /**
     * 删除销售订单明细
     */
    @Log(title = "删除销售明细", businessType = BusinessType.DELETE)
    @PostMapping("/order/detail/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hhOrderDetailService.deleteHhOrderDetailByIds(ids));
    }


    /**
     * 打印销售订单
     */
    @Log(title = "打印订单", businessType = BusinessType.OTHER)
    @GetMapping("/order/detail/print/{orderId}")
    public String orderPrint(@PathVariable("orderId") String orderId, ModelMap mmap) {
        Map resultMap = hhOrderDetailService.orderPrint(orderId);
        mmap.put("hhOrder", resultMap.get("hhOrder"));
        mmap.put("totalMoney", resultMap.get("totalMoney"));
        mmap.put("thisPay", resultMap.get("thisPay"));
        mmap.put("beforeBalance", resultMap.get("beforeBalance"));
        mmap.put("afterBalance", resultMap.get("afterBalance"));
        mmap.put("pageList", resultMap.get("pageList"));
        return prefix + "/print_view";
    }
}
